package cn.org.eshow.service.impl;

import cn.org.eshow.bean.query.ProductCategoryQuery;
import cn.org.eshow.bean.query.ProductQuery;
import cn.org.eshow.common.page.Page;
import cn.org.eshow.dao.ProductCategoryDao;
import cn.org.eshow.dao.ProductDao;
import cn.org.eshow.model.Product;
import cn.org.eshow.model.ProductCategory;
import cn.org.eshow.service.ProductCategoryManager;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.jws.WebService;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@WebService(serviceName = "ProductCategoryService", endpointInterface = "cn.org.eshow.service.ProductCategoryManager")
@Service
public class ProductCategoryManagerImpl extends GenericManagerImpl<ProductCategory, Integer> implements ProductCategoryManager {

    @Autowired
    ProductCategoryDao productCategoryDao;
    @Autowired
    ProductDao productDao;

    public ProductCategoryManagerImpl() {
    }

    @Autowired
    public ProductCategoryManagerImpl(ProductCategoryDao productCategoryDao) {
        super(productCategoryDao);
        this.productCategoryDao = productCategoryDao;
    }

    @Override
    public List<ProductCategory> list(ProductCategoryQuery query) {
        return productCategoryDao.list(query);
    }

    @Override
    public Page<ProductCategory> search(ProductCategoryQuery query) {
        return productCategoryDao.search(query);
    }

    @Override
    public ProductCategory save(ProductCategory productCategory, Integer sequence) {
        productCategory.setSequence(sequence == null ? 0 : sequence);
        productCategory.setEnabled(Boolean.TRUE);
        return productCategoryDao.save(productCategory);
    }

    @Override
    public ProductCategory check(ProductCategoryQuery query) {
        List<ProductCategory> list = productCategoryDao.list(query);
        if (!list.isEmpty()) {
            return list.get(0);
        }
        return null;
    }

    @Override
    public long count(ProductCategoryQuery query) {
        return productCategoryDao.count(query);
    }

    @Override
    public void remove(ProductCategory productCategory) {
        ProductQuery query = new ProductQuery();
        query.setProductCategoryId(productCategory.getId());
        List<Product> products = productDao.list(query);
        if (!products.isEmpty()) {
            List<Product> results = new ArrayList<Product>();
            for (Product product : products) {
                product.setProductCategory(null);//对应商品分类下所有商品的分类置为空
                results.add(product);
            }
            productDao.save(results);
        }
        productCategoryDao.remove(productCategory);
    }

    @Override
    public ProductCategory addChild(ProductCategory parentCategory,String childName) {
        parentCategory.setUpdateTime(new Date());
        if(parentCategory.getChildrenSize()==null){
            parentCategory.setChildrenSize(0);
        }
        parentCategory.setChildrenSize(parentCategory.getChildrenSize()+1);
        parentCategory=productCategoryDao.save(parentCategory);
        ProductCategory child=new ProductCategory();
        child.setChildrenSize(0);
        if(!StringUtils.isEmpty(childName)){
            child.setName(childName);
        }else {
            child.setName("新建节点");
        }
        child.setParent(parentCategory);
        child=productCategoryDao.save(child);

        return  child;
    }

}
